package org.luxor.digital.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.luxor.commons.core.validator.group.AddGroup;
import org.luxor.commons.core.validator.group.UpdateGroup;
import org.luxor.commons.core.web.controller.BaseRestController;
import org.luxor.commons.core.web.data.R;
import org.luxor.commons.jdbc.metadata.PageQuery;
import org.luxor.digital.entity.SysDictionaryCategoryEntity;
import org.luxor.digital.service.ISysDictionaryCategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;

/**
 * 字典分类表
 *
 * @author Mark @date 2020-07-26 14:19:09
 */
@RestController
@RequestMapping("/digital/sysDictionaryCategory")
@Api(tags = "字典分类表")
public class SysDictionaryCategoryController extends BaseRestController {

    @Resource
    private ISysDictionaryCategoryService sysDictionaryCategoryService;

    @ApiOperation("分页列表")
    @PostMapping("/list")
    @PreAuthorize("hasPermission(#request,'sys:digital:dictionaryCategory:list')")
    public R<IPage<SysDictionaryCategoryEntity>> list(@RequestBody PageQuery<HashMap> searchPage) {
        IPage<SysDictionaryCategoryEntity> data = sysDictionaryCategoryService.page(searchPage.getPage(), searchPage.getSearch());
        return R.ok(data);
    }

    @ApiOperation("详情信息")
    @ApiImplicitParam(name = "dicId", value = "主键ID", paramType = "path", required = true)
    @GetMapping("/info/{dicId}")
    @PreAuthorize("hasPermission(#request,'sys:digital:dictionaryCategory:info')")
    public R<SysDictionaryCategoryEntity> info(@PathVariable("dicId") Long dicId) {
        SysDictionaryCategoryEntity sysDictionaryCategory = sysDictionaryCategoryService.getById(dicId);
        return R.ok(sysDictionaryCategory);
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    @PreAuthorize("hasPermission(#request,'sys:digital:dictionaryCategory:save')")
    public R<SysDictionaryCategoryEntity> save(@Validated(AddGroup.class) @RequestBody SysDictionaryCategoryEntity sysDictionaryCategory) {
        sysDictionaryCategoryService.save(sysDictionaryCategory);
        return R.ok(sysDictionaryCategory);
    }

    @ApiOperation("修改")
    @PutMapping("/update")
    @PreAuthorize("hasPermission(#request,'sys:digital:dictionaryCategory:update')")
    public R<SysDictionaryCategoryEntity> update(@Validated(UpdateGroup.class) @RequestBody SysDictionaryCategoryEntity sysDictionaryCategory) {
        sysDictionaryCategoryService.updateById(sysDictionaryCategory);
        return R.ok(sysDictionaryCategory);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete")
    @PreAuthorize("hasPermission(#request,'sys:digital:dictionaryCategory:delete')")
    public R delete(@RequestBody Long[] ids) {
        sysDictionaryCategoryService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
